package com.oracle.malldemo.servlet.order;

import com.alibaba.fastjson.JSONObject;
import com.oracle.malldemo.domain.OrderMain;
import com.oracle.malldemo.util.MyUtil;
import com.oracle.malldemo.view.JsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/order/qrcode")
public class WeiPayQRCodeServlet extends OrderServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/plain;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        String oid = req.getParameter("oid");
        OrderMain orderMain = orderMainService.queryOrderMainByOid(oid);
        try {
            String qRcodeImgUrl = MyUtil.weipay(orderMain.getOid(), orderMain.getTotalPrice(), orderMain.getRealname() + "的订单");
            JsonData result = JsonData.success("生成支付二维码成功！", qRcodeImgUrl);
            out.write(JSONObject.toJSONString(result));
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            JsonData result = JsonData.fail("生成支付二维码失败！");
            out.write(JSONObject.toJSONString(result));
            out.flush();
            out.close();
        }
    }
}
